package com.wuiy.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wuiy.util.JSONUtils;
import com.wuiy.indexdemo.R;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class EnviorFragment extends Fragment {
    private String SURL = "http://192.168.2.5:8080/transportservice/action/GetAllSense.do";
    private View                mView;
    private Map<String, String> mDataMap;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            setData(mDataMap);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_envior, null);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final URL OURL = new URL(SURL);
                    Timer timer = new Timer();
                    TimerTask timerTask = new TimerTask() {
                        @Override
                        public void run() {
                            try {
                                HttpURLConnection httpURLConnection = (HttpURLConnection) OURL.openConnection();
                                httpURLConnection.setRequestMethod("POST");
                                httpURLConnection.setReadTimeout(5000);
                                if (httpURLConnection.getResponseCode() == 200) {
                                    InputStream inputStream = httpURLConnection.getInputStream();
                                    mDataMap = JSONUtils.getDataMap(inputStream);
                                    mHandler.sendMessage(Message.obtain());
                                }
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                    };
                    timer.schedule(timerTask, 1, 5000);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
        return mView;
    }

    @Override
    public void onPause() {
        super.onPause();
        onDestroy();
    }

    private void setData(Map<String, String> dataMap) {
        TextView tempTV = mView.findViewById(R.id.temp);
        tempTV.setText(dataMap.get("temperature"));
        TextView humiTV = mView.findViewById(R.id.humi);
        humiTV.setText(dataMap.get("humidity"));
        TextView lightTV = mView.findViewById(R.id.illum);
        lightTV.setText(dataMap.get("LightIntensity"));
        TextView co2TV = mView.findViewById(R.id.co2);
        co2TV.setText(dataMap.get("co2"));
        TextView pm2_5TV = mView.findViewById(R.id.pm2_5);
        pm2_5TV.setText(dataMap.get("pm2.5"));
    }
}
